﻿using System;
using System.Collections.Generic;

class LongestSequenceOfStrings
{
    static List<string> bestSequence = new List<string>();

    static string[,] array = 
    {
        { "ha", "fifi", "ho", "hi" },
        { "fo", "ha", "hi", "xx" },
        { "xxx", "ho", "ha", "xx" },
    };
    
    static void Main()
    {
        for (int i = 0; i < array.GetLength(0); i++)
        {
            for (int j = 0; j < array.GetLength(1); j++)
            {
                Generator(i, j, 0, 1);
                Generator(i, j, 1, 1);
                Generator(i, j, 1, 0);
                Generator(i, j, 0, -1);
            }
        }
        Console.WriteLine("Най дългата редица е:");
        Print(bestSequence);
        Console.WriteLine("Дължината и е: {0}", bestSequence.Count);
    }
  
    private static void Generator(int i, int j, int k, int l)
    {
        List<string> candidateSequence = new List<string>();
        candidateSequence.Add(array[i, j]);
        CheckBestSequence(candidateSequence, i, j, k, l);
    }

    private static void CheckBestSequence(List<string> candidateSequence, int indexX, int indexY, int deltaX, int deltaY)
    {
        if (isInRange(indexX + deltaX, indexY + deltaY))
        {
            if (array[indexX, indexY] == array[indexX + deltaX, indexY + deltaY])
            {
                candidateSequence.Add(array[indexX + deltaX, indexY + deltaY]);
                CheckBestSequence(candidateSequence, indexX + deltaX, indexY + deltaY, deltaX, deltaY);
            }
        }
        if (candidateSequence.Count > bestSequence.Count)
        {
            bestSequence = candidateSequence;
        }
    }

    static bool isInRange(int indexX, int indexY)
    {
        if (indexX < array.GetLength(0) && indexX >= 0 && indexY < array.GetLength(1) && indexY >= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    static void Print(List<string> array)
    {
        for (int i = 0; i < array.Count; i++)
        {
            Console.Write(array[i] + " ");            
        }
        Console.WriteLine();
    }
}